package cn.zhy.org.service.impl;

import cn.zhy.basic.service.impl.BaseServiceImpl;
import cn.zhy.basic.util.MD5Utils;
import cn.zhy.basic.util.PageList;
import cn.zhy.basic.util.StrUtils;
import cn.zhy.org.domain.Employee;
import cn.zhy.org.mapper.EmployeeMapper;
import cn.zhy.org.query.EmployeeQuery;
import cn.zhy.org.service.IEmployeeService;
import cn.zhy.user.domain.Logininfo;
import cn.zhy.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;



    @Override
    public void save(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //将employy属性转换为logininfo
        Logininfo logininfo= employee2Logininfo(employee);
        //将logininfo添加到数据库
        logininfoMapper.save(logininfo);
        //employy里设置ligininfoid
        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);
        //保存中间表的信息
        employeeMapper.saveEmployeeRole(employee);


    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        return logininfo;
    }


    @Override
    public void remove(Long id) {
        Employee employee = employeeMapper.loadById(id);
        Long logininfo_id = employee.getLogininfo_id();
        logininfoMapper.remove(logininfo_id);
        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRoleByEmployeeId(id);
        super.remove(id);
    }


    @Override
    public void update(Employee employee) {
        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //将employy属性转换为logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        //设置login infoid
        logininfo.setId(logininfo_id);
        logininfoMapper.update(logininfo);
        super.update(employee);

        //删除中间表信息
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());
        //保存新的信息
        employeeMapper.saveEmployeeRole(employee);

    }







}
